//
//  BaseNode.swift
//  HelloSwiftUI
//
//  Created by kuangxianming on 2024/6/5.
//

import Foundation
import SceneKit

/*
    该节点是一个复合节点
    负责：
        1.节点坐标系显示
        2.选中处理
        3.旋转、位移处理
 */
class BaseNode: SCNNode
{
    var xAxisNode: SCNNode?
    var yAxisNode: SCNNode?
    var zAxisNode: SCNNode?
    
    required override init() {
        super.init()
        
        self.addAxis()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func addAxis() {
        // 定义坐标轴长度和线宽
        let axisLength: CGFloat = 10.0
        let lineWidth: CGFloat = 0.05

        // 创建X轴（红色）
        let xAxisGeometry = SCNBox(width: lineWidth, height: axisLength, length: lineWidth, chamferRadius: 0)
        xAxisGeometry.firstMaterial?.diffuse.contents = UIColor.red
        let xAxisNode = SCNNode(geometry: xAxisGeometry)
        xAxisNode.rotation = SCNVector4(0, 0, 1, -Float.pi / 2) // 旋转X轴水平指向右
//        xAxisNode.position = SCNVector3(axisLength / 2, 0, 0) // 移动到X轴末端
        self.addChildNode(xAxisNode)

        // 创建Y轴（绿色）
        let yAxisGeometry = SCNBox(width: lineWidth, height: axisLength, length: lineWidth, chamferRadius: 0)
        yAxisGeometry.firstMaterial?.diffuse.contents = UIColor.green
        let yAxisNode = SCNNode(geometry: yAxisGeometry) // Y轴默认竖直
//        yAxisNode.position = SCNVector3(0, axisLength / 2, 0) // 移动到Y轴末端
        self.addChildNode(yAxisNode)

        // 创建Z轴（蓝色）
        let zAxisGeometry = SCNBox(width: lineWidth, height: axisLength, length: lineWidth, chamferRadius: 0)
        zAxisGeometry.firstMaterial?.diffuse.contents = UIColor.blue
        let zAxisNode = SCNNode(geometry: zAxisGeometry)
        zAxisNode.rotation = SCNVector4(1, 0, 0, -Float.pi / 2) // 旋转Z轴使其垂直屏幕
//        zAxisNode.position = SCNVector3(0, 0, axisLength / 2) // 移动到Z轴末端
        self.addChildNode(zAxisNode)
    }
}

